<template>
  <div>
    <el-form :inline="true" label-width="70px" :disabled="isShow">
      <el-form-item label="一级分类">
        <el-select v-model="category1Id" @change="getCategory2List" placeholder="请选择一级分类">
          <el-option
            v-for="item in category1List"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="category2Id" placeholder="请选择二级分类" @change="getCategory3List">
          <el-option
            v-for="item in category2List"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="category3Id" placeholder="请选择三级分类">
          <el-option
            v-for="item in category3List"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, defineProps } from 'vue'
import { useCategoryStore } from '@/stores/category'
import { storeToRefs } from 'pinia'

const categoryStore = useCategoryStore()
defineProps<{
  isShow: boolean
}>()

const { category1List, category1Id, category2Id, category2List, category3List, category3Id } =
  storeToRefs(categoryStore)
const getCategory2List = () => {
  // console.log(category1Id.value)
  category2List.value = category3List.value = []
  category2Id.value = category3Id.value = ''
  categoryStore.getCategory2(category1Id.value as number)
}

const getCategory3List = () => {
  category3Id.value = ''
  category3List.value = []
  categoryStore.getCategory3(category2Id.value as number)
}
onMounted(() => {
  categoryStore.getCategory1()
})
</script>

<script lang="ts">
export default {
  name: 'VCategory'
}
</script>

<style lang="scss" scoped></style>
